/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.GuiManager;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

/**
 *
 * @author Jojo
 */
public class ObjectCard extends javax.swing.JPanel {
    
    protected int cardId = 0;
    
    protected String objectName = "Test";

    /**
     * Creates new form ObjectCard
     */
    public ObjectCard(String pName, int pCardType) {
        initComponents();
        this.cardId = pCardType;
        this.objectName = pName;
        this.setPreferredSize(new Dimension(180,30));
        this.setSize(new Dimension(180,30));
        this.setMaximumSize(new Dimension(180,30));
        this.setMinimumSize(new Dimension(180,30));
        this.setBorder(BorderFactory.createEmptyBorder(5,15,5,15));  
    }
    
   
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setFont(new Font("Tahoma", Font.BOLD, 12)); 
        Dimension d = this.getSize();
        g2d.setColor(this.getBackground());
        g2d.fillRoundRect(0, 0, this.getBounds().width, this.getBounds().height, 25, 25);
        g2d.setColor(Color.white);
        g2d.fillRoundRect(0, 0, this.getBounds().width - 2, this.getBounds().height - 2, 25, 25);
        g2d.setColor(Color.black);
        g2d.drawRoundRect(0, 0, this.getBounds().width - 2, this.getBounds().height - 2, 25, 25);
        g2d.drawString(objectName,  (d.width/2)-(g.getFontMetrics().stringWidth(objectName)/2), 15);
        g2d.setFont(new Font("Tahoma", Font.PLAIN, 12));
    }
    
   protected String getUserChange(String pQuestion){
        String userInput = JOptionPane.showInputDialog(this.getParent().getParent().getParent(), pQuestion);
        if (userInput != null){
           return userInput;
        }
        else{
            return "";
        }

    }
   
   public void handleDoubleClick(){
       this.cardClicked();
   }
   
   public void showPopupmenu(int pX, int pY){
        this.jPopupMenu1.show(this,pX , pY);
    }
   
   void cardClicked(){
            String outputText = "";
            switch (this.cardId) {
            case 1:  outputText = "NxtBlock "+ objectName+" = new NxtBlock();"+"\n";
                     break;
            case 2:  outputText = "Kommunikation "+ objectName+" = new Kommunikation();"+"\n";
                     break;  
            case 8:  outputText = "Servomotor "+ objectName+" = new Servomotor(\"A\");"+"\n";
                     break;  
            default: outputText = "Fehler";
                     break;
        }
            GuiManager.getActivePane().insert(outputText, GuiManager.getActivePane().getCaretPosition());
        }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenuItem1.setText("ändere Objektbezeichner");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem1);

        jMenuItem2.setText("Objekt einfügen");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiInsertObjektClicked(evt);
            }
        });
        jPopupMenu1.add(jMenuItem2);

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                handleClickOnObjectCard(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void handleClickOnObjectCard(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_handleClickOnObjectCard
        if(evt.getButton()==MouseEvent.BUTTON3){
            this.showPopupmenu(evt.getX(),evt.getY());
            //JOptionPane.showInputDialog(this,"Rechtsklick!",null);
        }
        if (evt.getButton()==MouseEvent.BUTTON1 && evt.getClickCount() == 2){
            this.handleDoubleClick();
        }
    }//GEN-LAST:event_handleClickOnObjectCard

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.objectName = this.getUserChange("Wie soll der neue Objektbezeichner lauten?");
        this.repaint();
        this.validate();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jmiInsertObjektClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiInsertObjektClicked
       this.handleDoubleClick();
    }//GEN-LAST:event_jmiInsertObjektClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    protected javax.swing.JPopupMenu jPopupMenu1;
    // End of variables declaration//GEN-END:variables
}